import time
from la import models


""" 判断一个sku集合是否在数据库中 """

def ishavesku(skulist):
    for sku in skulist:
        if sku['款号'] != '款号':
            re = models.skuinfo.objects.filter(sku=sku['款号']).first()
            if re != None:
                continue
            else:
                return False
    return True

""" 判断一个sku集合是否在数据库中超库存 """

def ishaveskuandnum(skulist,storeid):
    for sku in skulist:
        if sku['款号'] != '款号':
            re = models.inventory.objects.filter(sku=sku['款号'], num__gte=sku['数量'],storecode_id=storeid).first()
            if re != None:
                continue
            else:
                return False
    return True

"""设置单据编号"""
def getPID(req):
    '''设置销售单据编号'''
    sn = "S"
    info = req.session.get( "info" )
    sn = sn + str( info['storecode'] )
    dt = time.strftime( "%Y-%m-%d", time.localtime() )
    dt2 = time.strftime( "%Y%m%d", time.localtime() )
    sale = models.salelist.objects.filter( time__contains=dt ).last()
    if sale == None:
        sn = sn + str( dt2 ) + "0001"
        return sn
    else:
        pid = str(sale.pid)
        n = pid[-4:]
        n = int(n)
        n = n + 1
        n = str(n).rjust(4,"0")
        sn = pid[0:10]+n
        sn = sn[1:]
        sn = "S" + sn
        return sn
def getDPID(req):
    '''设置退货单据编号'''
    sn = "D"
    info = req.session.get( "info" )
    sn = sn + str( info['storecode'] )
    dt = time.strftime( "%Y-%m-%d", time.localtime() )
    dt2 = time.strftime( "%Y%m%d", time.localtime() )
    sale = models.salelist.objects.filter( time__contains=dt ).last()
    if sale == None:
        sn = sn + str( dt2 ) + "0001"
        return sn
    else:
        pid = str(sale.pid)
        n = pid[-4:]
        n = int(n)
        n = n + 1
        n = str(n).rjust(4,"0")
        sn = pid[0:10]+n
        sn = sn[1:]
        sn = "D" + sn
        return sn